#include <iostream>
#include <stdexcept>

class MyArray {
private:
    int* p;
    int N;

public:
    MyArray() : p(nullptr), N(0) {
        std::cout << "默认构造函数" << std::endl;
    }
    
    MyArray(int size) {
        std::cout << "指定大小构造函数，大小: " << size << std::endl;
        if (size < 0) {
            N = 0;
            p = nullptr;
        } else {
            N = size;
            p = new int[N];
            for (int i = 0; i < N; i++) {
                p[i] = 0;
            }
        }
    }
    
    MyArray(const MyArray& other) {
        std::cout << "拷贝构造函数" << std::endl;
        N = other.N;
        if (N > 0) {
            p = new int[N];
            for (int i = 0; i < N; i++) {
                p[i] = other.p[i];
            }
        } else {
            p = nullptr;
        }
    }
    
    ~MyArray() {
        delete[] p;
    }
    
    MyArray combine(const MyArray& other) const {
        MyArray result(N + other.N);
        
        for (int i = 0; i < N; i++) {
            result.p[i] = p[i];
        }
        
        for (int i = 0; i < other.N; i++) {
            result.p[N + i] = other.p[i];
        }
        
        return result;
    }
    
    MyArray add(const MyArray& other) const {
        if (N != other.N) {
            throw std::invalid_argument("数组大小不一致，无法进行加法运算");
        }
        
        MyArray result(N);
        for (int i = 0; i < N; i++) {
            result.p[i] = p[i] + other.p[i];
        }
        return result;
    }
    
    MyArray subtract(const MyArray& other) const {
        if (N != other.N) {
            throw std::invalid_argument("数组大小不一致，无法进行减法运算");
        }
        
        MyArray result(N);
        for (int i = 0; i < N; i++) {
            result.p[i] = p[i] - other.p[i];
        }
        return result;
    }
    
    MyArray multiply(const MyArray& other) const {
        if (N != other.N) {
            throw std::invalid_argument("数组大小不一致，无法进行乘法运算");
        }
        
        MyArray result(N);
        for (int i = 0; i < N; i++) {
            result.p[i] = p[i] * other.p[i];
        }
        return result;
    }
    
    MyArray divide(const MyArray& other) const {
        if (N != other.N) {
            throw std::invalid_argument("数组大小不一致，无法进行除法运算");
        }
        
        MyArray result(N);
        for (int i = 0; i < N; i++) {
            if (other.p[i] == 0) {
                throw std::invalid_argument("除数不能为零");
            }
            result.p[i] = p[i] / other.p[i];
        }
        return result;
    }
    
    int size() const {
        return N;
    }
    
    void setValue(int index, int value) {
        if (index >= 0 && index < N) {
            p[index] = value;
        }
    }
    
    int getValue(int index) const {
        if (index >= 0 && index < N) {
            return p[index];
        }
        return 0;
    }
    
    void display() const {
        std::cout << "[";
        for (int i = 0; i < N; i++) {
            std::cout << p[i];
            if (i < N - 1) {
                std::cout << ", ";
            }
        }
        std::cout << "]" << std::endl;
    }
};

int main() {
    std::cout << "=== MyArray 类功能测试 ===" << std::endl;
    
    std::cout << "\n1. 构造函数测试:" << std::endl;
    MyArray arr1;
    std::cout << "默认构造数组大小: " << arr1.size() << std::endl;
    
    MyArray arr2(5);
    std::cout << "指定大小(5)数组: ";
    arr2.display();
    
    std::cout << "\n2. 设置和获取值测试:" << std::endl;
    for (int i = 0; i < 5; i++) {
        arr2.setValue(i, i * 10);
    }
    std::cout << "设置值后的数组: ";
    arr2.display();
    std::cout << "索引2的值: " << arr2.getValue(2) << std::endl;
    
    std::cout << "\n3. 拷贝构造函数测试:" << std::endl;
    MyArray arr3 = arr2;
    std::cout << "原数组: ";
    arr2.display();
    std::cout << "拷贝数组: ";
    arr3.display();
    
    std::cout << "\n4. 数组合并测试:" << std::endl;
    MyArray arr4(3);
    for (int i = 0; i < 3; i++) {
        arr4.setValue(i, i + 100);
    }
    std::cout << "数组A: ";
    arr2.display();
    std::cout << "数组B: ";
    arr4.display();
    
    MyArray combined = arr2.combine(arr4);
    std::cout << "合并后: ";
    combined.display();
    
    std::cout << "\n5. 四则运算测试:" << std::endl;
    MyArray arr5(4);
    MyArray arr6(4);
    
    for (int i = 0; i < 4; i++) {
        arr5.setValue(i, (i + 1) * 2);
        arr6.setValue(i, i + 1);
    }
    
    std::cout << "数组A: ";
    arr5.display();
    std::cout << "数组B: ";
    arr6.display();
    
    std::cout << "加法结果: ";
    arr5.add(arr6).display();
    
    std::cout << "减法结果: ";
    arr5.subtract(arr6).display();
    
    std::cout << "乘法结果: ";
    arr5.multiply(arr6).display();
    
    std::cout << "除法结果: ";
    arr5.divide(arr6).display();
    
    std::cout << "\n6. 异常情况测试:" << std::endl;
    MyArray arr7(3);
    try {
        std::cout << "测试大小不一致的加法: ";
        arr5.add(arr7);
    } catch (const std::exception& e) {
        std::cout << "异常捕获: " << e.what() << std::endl;
    }
    
    MyArray zeroArray(2);
    zeroArray.setValue(0, 10);
    zeroArray.setValue(1, 0);
    try {
        std::cout << "测试除零异常: ";
        arr5.divide(zeroArray);
    } catch (const std::exception& e) {
        std::cout << "异常捕获: " << e.what() << std::endl;
    }
    
    std::cout << "\n=== 测试完成 ===" << std::endl;
    return 0;
}
